package dst.ass1.jpa.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;

import dst.ass1.jpa.dao.IExpertDAO;
import dst.ass1.jpa.model.IExpert;
import dst.ass1.jpa.model.impl.Expert;
import dst.ass1.jpa.util.Constants;

public class ExpertDAO extends GenericJpaDAO <IExpert> implements IExpertDAO {

	private final static Logger LOGGER = Logger.getLogger(ExpertDAO.class.getName());
	
	public ExpertDAO(Session session) {
        super(session);
    }

    @Override
    protected String getTableName() {
        return Expert.class.getSimpleName();
    }
	
	@Override
	public HashMap<IExpert, Date> findNextTaskForceMeetingOfExperts() {
		HashMap<IExpert, Date> res= new HashMap<IExpert, Date>();
		
		LOGGER.info("findNextTaskForceMeetingOfExperts called");
		
		Query q=this.getSession().getNamedQuery(Constants.Q_TASKFORCESOFEXPERT);
		
		/**
		 * Java Part: 1.2.b
		 * AVOID K+1 Select Problem --> no extra db access in for loop
		 * can be expensive for many experts that are found
		 * otherwise: for each Expert the Taskforce meeting date have to be loaded in the loop
		 */
		@SuppressWarnings("unchecked")
		List<HashMap<String,Object>> l=q.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();

		//mapping from string,object to expert,date hashmap
		for(HashMap<String,Object> hm: l){
			Expert exp=(Expert)hm.get("expert");		//getting the value
			Date dat=(Date)hm.get("meeting");			//getting the value
			

			LOGGER.info("ExpertDAO: "+exp.getFirstName()+" "+dat+" received");
			
			res.put(exp, dat);		//add in HashMap
				
		}
		
		LOGGER.info("findNextTaskForceMeetingOfExperts "+res.size() +" elements received");
		
		return res;
	}



}
